package com.som.service.excel.data1.process;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang3.StringUtils;

import com.som.bean.data1.input.DeviceCollectInfo;
import com.som.bean.data1.input.NeAreaInfo;
import com.som.bean.data1.output.IpaddressJoin;
import com.som.console.ExcelProcException;
import com.som.console.PathEnum;
import com.som.service.excel.ExcelConvDataProcessInf;
import com.som.service.excel.ExcelReadProcess;
import com.som.service.excel.data1.input.DeviceCollectExcelRead;
import com.som.service.excel.data1.input.NeAreaExcelRead;

/**
 * 
 * 以及通过LOOPBACK地址让数据二与数据三关联
 * 
 * @author liujun
 * @date 2016年4月8日
 * @verion 0.0.1
 */
public class Delete_IpaddressJoinProcess implements ExcelConvDataProcessInf<IpaddressJoin> {
    /**
     * 输入数据三：设备采集的数据(输入的文件名为input3.xls)
     */
    private ExcelReadProcess<String, DeviceCollectInfo> deviceCollProc = new DeviceCollectExcelRead();

    /**
     * 输入数据二：网元和区县对应表(输入文件名为input2.xls)
     */
    private ExcelReadProcess<String, NeAreaInfo> neAreaReadProc = new NeAreaExcelRead();

    /**
     * @param pathMap
     * @return
     * @throws ExcelProcException
     */
    public Map<String, IpaddressJoin> convProcessData(Map<String, String> pathMap) throws ExcelProcException {
        Map<String, NeAreaInfo> neAreaMap = null;
        String path = pathMap.get(PathEnum.DATA1_INPUT_NEAREAINFO.getPathFlag());

        // 读取网元和区县对应信息
        if (StringUtils.isNoneBlank(path)) {
            neAreaMap = neAreaReadProc.readExcelDataMap(path, 0, true);
        }

        Map<String, DeviceCollectInfo> deviceMap = null;
        String pathdevice = pathMap.get(PathEnum.DATA1_INPUT_DEVICECOLL.getPathFlag());

        // 读取设备采集的数据
        if (StringUtils.isNoneBlank(pathdevice)) {
            deviceMap = deviceCollProc.readExcelDataMap(pathdevice, 0, true);
        }

        // 进行数据逻辑合并
        if (null != deviceMap && neAreaMap != null) {
            Map<String, IpaddressJoin> ipJoinList = new LinkedHashMap<String, IpaddressJoin>();

            Iterator<Entry<String, DeviceCollectInfo>> deviceIter = deviceMap.entrySet().iterator();

            Entry<String, DeviceCollectInfo> entryDev = null;
            DeviceCollectInfo device = null;
            IpaddressJoin ipJoin = null;
            NeAreaInfo neArea = null;

            while (deviceIter.hasNext()) {
                entryDev = deviceIter.next();
                device = entryDev.getValue();

                // 通过loopBack地址获取对应数据
                neArea = neAreaMap.get(entryDev.getKey());

                ipJoin = new IpaddressJoin();
                ipJoin.setIpaddress(device.getIpaddress());
                ipJoin.setNetwork(device.getNetwork());

                // 当数据能找到时才进行填充
                if (null != neArea) {
                    ipJoin.setNeInfo(neArea.getName());
                    ipJoin.setAreainfo(neArea.getArea());
                }

                ipJoinList.put(ipJoin.getIpaddress(), ipJoin);
            }
            return ipJoinList;
        }

        return null;
    }

}
